﻿
using AutoMapper;
using EventBusETO.RoomType;
using Newtonsoft.Json;
using SH.Common.CommonReturn;
using SH.Order.Application.IServices.Dto;
using SH.Order.Application.IServices.IClientcu;
using SH.Order.Domain.OrderClass;
using SH.Order.Infrastructure.IRepository;
using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace SH.Order.Application.Services
{
    public class ClientcuServices : BaseServices<ClientcuStomer>, IClientcuServices
    {
        private readonly IClientcuRepository _clientcuRepository;
        private readonly IHttpClientFactory _httpclientFactory;
        private readonly IDicRepository _dicicRepository;
        private readonly IMapper _mapper;
        public ClientcuServices(IClientcuRepository clientcuRepository, IMapper mapper, IHttpClientFactory httpclientFactory, IDicRepository dicicRepository) : base(clientcuRepository)
        {
            _clientcuRepository = clientcuRepository;
            _mapper = mapper;
            _httpclientFactory = httpclientFactory;
            _dicicRepository = dicicRepository;
        }
        private const string getRoomList = "http://1.92.78.23:9003/api/RoomType/GetRoomList";
        private const string getRoomNumList = "http://1.92.78.23:9003/api/RoomNum/IRoomNum";
        /// <summary>
        /// 办理预定
        /// </summary>
        /// <param name="clientcuStomerdto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResult<int>> AddClientcu(ClientcuStomerDto clientcuStomerdto)
        {
            ApiResult<int> apiResult = new ApiResult<int>();
            var clientStomer = _mapper.Map<ClientcuStomer>(clientcuStomerdto);
            clientStomer.TenantId = "zhangsan";
            await _clientcuRepository.InsertAsync(clientStomer);
            apiResult.Success();
            return apiResult;
        }
        /// <summary>
        /// 显示房型和房间列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<RoomDto> RoomShow()
        {
            //房型
            var roomtype = _httpclientFactory.CreateClient();
            var roomresponse = roomtype.GetAsync(getRoomList).Result;

            if (roomresponse.IsSuccessStatusCode)
            {
                //房型
                var room = roomresponse.Content.ReadAsStringAsync().Result;
                var roomTypelist = JsonConvert.DeserializeObject<List<RoomTypeEto>>(room);


                var roomList = (from a in roomTypelist

                                select new RoomDto
                                {
                                    Rtid = a.Rtid,
                                    Rtname = a.Rtname,
                                    Rprice = a.Rprice,
                                    RoomApartment = a.RoomCount,
                                    Deposit = a.Deposit,
                                }).ToList();
                return roomList;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 房号显示
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<RoomNumDto> RoomNumShow(int id)
        {
            var roomnumtype = _httpclientFactory.CreateClient();
            var roomnumresponse = roomnumtype.GetAsync(getRoomNumList).Result;
            if (roomnumresponse.IsSuccessStatusCode)
            {
                var roomNum = roomnumresponse.Content.ReadAsStringAsync().Result;
                var roomNumTypeList = JsonConvert.DeserializeObject<List<RoomNumDto>>(roomNum);
                var roomNumList = roomNumTypeList.Where(a => a.Rtid == id).ToList();
                return roomNumList;
            }
            else
            {
                return null;
            }
        }


        public List<ClientcuStomer> clientcuShow()
        {
            var list = _clientcuRepository.GetList();
            return list;
        }
        /// <summary>
        /// 显示住宿
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="StateId">订单状态查询</param>
        /// <returns></returns>
        public ApiPaging<List<ClientcuStomerDto>> ClientcuStomersShow(DateTime? StartTime, DateTime? EndTime, int pageIndex, int pageSize, int StateId = -1)
        {
            var ClientcuStomerslist = _clientcuRepository.All;
            var Diclist = _dicicRepository.GetDics();
            Func<ClientcuStomerDto, bool> filter = a => true;

            filter = x => (StartTime == null || x.CheckTime >= StartTime) && (EndTime == null || x.DepartureTime < EndTime.Value.AddDays(1))
          ;
            //房型
            var roomnumtype = _httpclientFactory.CreateClient();
            var roomnumresponse = roomnumtype.GetAsync(getRoomList).Result;
            var roomtype = _httpclientFactory.CreateClient();
            var roomresponse = roomnumtype.GetAsync(getRoomNumList).Result;
            if (roomnumresponse.IsSuccessStatusCode && roomresponse.IsSuccessStatusCode)
            {
                //房型
                var roomNum = roomnumresponse.Content.ReadAsStringAsync().Result;
                var roomNumTypeList = JsonConvert.DeserializeObject<List<RoomDto>>(roomNum);
                var room = roomresponse.Content.ReadAsStringAsync().Result;
                var roomTypelist = JsonConvert.DeserializeObject<List<RoomNumDto>>(room);
                var list = (from a in ClientcuStomerslist.ToList()
                            join b in roomNumTypeList on a.Rtid equals b.Rtid
                            join c in roomTypelist on a.Roomid equals c.RoomId
                            join d in Diclist on a.State equals d.Id
                            select new ClientcuStomerDto
                            {
                                Id = a.Id,//预定id
                                Passenger = a.Passenger,//
                                Phone = a.Phone,
                                Roomid = a.Roomid,
                                Rtid = b.Rtid,
                                RoomNumberName = c.RoomNumber,
                                PassengerName = d.Value,
                                Rtname = b.Rtname,
                                State = a.State,
                                CheckTime = a.CheckTime,
                                DepartureTime = a.DepartureTime,
                                RoomNumber = a.RoomNumber,
                                MsId = a.MsId,
                                Rsid = a.Rsid,
                                NumBerday = a.NumBerday,
                                OrderSource = a.OrderSource,
                                truename = a.truename,
                                ReserVation = a.ReserVation,
                                Remark = a.Remark,


                            }).ToList();
                int totalCount = 0;
                int pageCount = 0;
                list = list.Where(filter).ToList();
                totalCount = list.Count;
                pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
                list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                ApiPaging<List<ClientcuStomerDto>> apiPaging = new ApiPaging<List<ClientcuStomerDto>>();
                apiPaging.Data = list;
                apiPaging.TotalCount = totalCount;
                apiPaging.PageCount = pageCount;
                return apiPaging;
            }
            else
            {
                return null;
            }

        }

        public List<ClientcuStomerDto> ClientcuStomersFind(int id)
        {
            var ClientcuStomerslist = _clientcuRepository.All;
            var Diclist = _dicicRepository.GetDics();
            Func<ClientcuStomerDto, bool> filter = a => true;

            filter = x => ( x.Id == id);
            //房型
            var roomnumtype = _httpclientFactory.CreateClient();
            var roomnumresponse = roomnumtype.GetAsync(getRoomList).Result;
            var roomtype = _httpclientFactory.CreateClient();
            var roomresponse = roomnumtype.GetAsync(getRoomNumList).Result;
            if (roomnumresponse.IsSuccessStatusCode && roomresponse.IsSuccessStatusCode)
            {
                //房型
                var roomNum = roomnumresponse.Content.ReadAsStringAsync().Result;
                var roomNumTypeList = JsonConvert.DeserializeObject<List<RoomDto>>(roomNum);
                var room = roomresponse.Content.ReadAsStringAsync().Result;
                var roomTypelist = JsonConvert.DeserializeObject<List<RoomNumDto>>(room);
                var list = (from a in ClientcuStomerslist.ToList()
                            join b in roomNumTypeList on a.Rtid equals b.Rtid
                            join c in roomTypelist on a.Roomid equals c.RoomId
                            join d in Diclist on a.State equals d.Id
                            select new ClientcuStomerDto
                            {
                                Id = a.Id,//预定id
                                Passenger = a.Passenger,//
                                Phone = a.Phone,
                                Roomid = a.Roomid,
                                Rtid = b.Rtid,
                                RoomNumberName = c.RoomNumber,
                                PassengerName = d.Value,
                                Rtname = b.Rtname,
                                State = a.State,
                                CheckTime = a.CheckTime,
                                DepartureTime = a.DepartureTime,
                                RoomNumber = a.RoomNumber,
                                MsId = a.MsId,
                                Rsid = a.Rsid,
                                NumBerday = a.NumBerday,
                                OrderSource = a.OrderSource,
                                truename = a.truename,
                                ReserVation = a.ReserVation,
                                Remark = a.Remark,
                       
                            }).ToList();
                list = list.Where(filter).ToList();
                return list;
            }
            else
            {
                return null;
            }
        }
    }
}
